package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.AppIdChaneelInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/queryChannelServerList")
public class QueryChannelServerList extends BaseJsonHttpServlet implements Serializable {

    private static final long serialVersionUID = -6665912671909388238L;

    private static Logger logger = LogManager.getLogger(QueryChannelServerList.class.getName());

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("request queryChannelServerList");
        HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
        JSONObject paramObj = httpJsonWebParam.getParamObj();
        String appId = paramObj.getString("appid");
        if (StringUtils.isNullOrEmpty(appId)) {
            logger.error("queryChannelServerList argument error:" + appId);
            sendResult(response, 0, "appId args error");
            return;
        }
        if(WebServer.getInstance().isNeedSend() && ServerListMgr.is201AppId(appId)){
            // 转发消息到对应的服务器上
            String result = HttpHelper.doPostJson(ChannelConfig.LOGIN_SEND_URL + "queryChannelServerList", paramObj.toJSONString(), "UTF-8");
            sendMessage(response, result);
            return;
        }
        AppIdChaneelInfo chanelInfo = ServerListMgr.getAppIdChaneelInfo(appId);
        if (chanelInfo == null) {
            logger.error("queryChannelServerList not chanelInfo:" + appId);
            sendResult(response, 0, "appId args error");
            return;
        }
        List<ServerInfo> serverInfoList = ServerListMgr.getChannelServerList(chanelInfo.getChannelId());

        JSONObject resultObj = getResultObj(1, "");

        JSONArray serverListArr = new JSONArray();
        for (ServerInfo serverInfo : serverInfoList) {
            if (serverInfo.getServerType() != 0) {
                // 非普通服不下发
                continue;
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("dsid", serverInfo.getServerId());
            jsonObject.put("dsname", serverInfo.getServerName());
            jsonObject.put("start_time", DateHelper.getCurrentDateTimeString(serverInfo.getOpenTime() * 1000));

            int statue = 0;
            if (serverInfo.isOpen()) {
                // 服务器开启状态
                if (serverInfo.getOpenTime() * 1000 > System.currentTimeMillis()) {
                    // 开服时间大于当前时间 则表示期待
                    statue = 1;
                } else {
                    // 已对外的则表示对外
                    statue = 2;
                }
            }
            jsonObject.put("statue", statue);
            serverListArr.add(jsonObject);
        }
        resultObj.put("data", serverListArr);

        sendMessage(response, resultObj.toJSONString());
    }
}
